//
//  UIGridView.h
//  X-Touch2
//
//  Created by shengchao yang on 12-3-25.
//  Copyright (c) 2012年 foxhis. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "GridViewItem.h"

@protocol UIGridViewDataSource;
@protocol UIGridViewDelegate;

@interface UIGridView : UIView<UIScrollViewDelegate,UIGridViewItemDelegate>{
    id _targert;
    CGRect _frame;
    NSInteger _currentPageIndex;                            //当前第几页
    NSInteger _rows;                                        //行数
    NSInteger _cols;                                        //列数
    NSInteger _totalCount;                                  //总个数
    NSInteger _pages;                                       //总页数
    
    CGPoint _itemStartPoint;//默认(10,10)
    CGSize _itemMargin;//默认(10,10)
    CGSize _itemSize;//自动计算
    CGRect _controlFrame;//默认height=30;
    CGRect _scrollFrame;//默认height=frame.size.height-_controlFrame.size.height
    
    BOOL _pageControlEnable;          //是否使用pageControl控件
    BOOL _direction;                  //滑动的方向(YES-横向默认  NO-纵向)
    BOOL _showScrollIndicator;        //是否显示滑动条Indicator(默认NO)
    
    id<UIGridViewDataSource> dataSource;
    id<UIGridViewDelegate> delegate;
    
    UIScrollView *_scrollView;       //
    UIPageControl *_pageControl;     //
    NSMutableArray *_gridViewItems;  //存放item对象的数组
    
    NSInteger loadPageCount;         //前后加载page的个数
    NSMutableSet * _resuedGridItems; //存储重用的Item
}

@property (nonatomic, assign) id <UIGridViewDataSource> dataSource;
@property (nonatomic, assign) id <UIGridViewDelegate>   delegate;
-(id)initWithTarget:(id)target withFrame:(CGRect)frame withTag:(NSInteger)tag;
-(void)setCurrentPageIndex:(NSInteger)index;
-(void)reloadData;
-(GridViewItem *)dequeueReusableCellWithIdentifier:(NSString *)identifier;
@end

@protocol UIGridViewDataSource
@required
-(NSInteger) gridViewRows:(UIGridView *)gridView;          //行数
-(NSInteger) gridViewCols:(UIGridView *)gridView;          //列数
-(NSInteger) gridViewTotalCount:(UIGridView *)gridView;    //总记录数
-(GridViewItem *) gridView:(UIGridView *)gridView itemAtIndex:(NSInteger)index;//返回一个GridViewItem对象

@optional
-(CGPoint) gridViewItemStartPoint:(UIGridView *)gridView;  //item开始位置
-(CGSize) gridViewItemMargin:(UIGridView *)gridView;       //item之间的间隔
-(CGSize) gridViewItemSize:(UIGridView *)gridView;         //item大小(该方法通常不需要重载)
-(CGRect) gridViewControlFrame:(UIGridView *)gridView;     //UIPageControl控件的位置和范围
-(CGRect) gridViewScrollFrame:(UIGridView *)gridView;      //UIScrollView的位置和范围

-(BOOL) gridViewPageControlEnable:(UIGridView *)gridView;  //是否使用pageControl控件
-(BOOL) gridViewDirection:(UIGridView *)gridView;          //滑动的方向(YES-横向默认  NO-纵向)
-(BOOL) gridViewShowScrollIndicator:(UIGridView *)gridView;//是否显示滑动条Indicator(默认NO)
@end


@protocol UIGridViewDelegate
@required
-(void) gridView:(UIGridView *)gridView didSelectItemAtIndex:(NSInteger)index;//点击item的回调函数
@end


